Skip to content

Conversation

@gottesmm
Copy link
Contributor

This should make it easier to add new builtins by "following the warnings" and
prevent us from not handling a builtin in IRGen.

When I did this, I discovered that if I did this naively, we would have
AddressOf show up twice in the switch. This turned out to be because:

  1. AddressOf is a SIL builtin that semantically is expected to only result in
    SIL being emitted instead of having a builtin "addressof" be emitted.

  2. For what ever reason, we actually had code in IRGen to emit an AddressOf
    BuiltinInst if we saw it (which we never should have)... but also later code
    asserted that we would never see it b/c it is a "SIL only builtin".

  3. When I converted the if statements to be case statements, helpfully the
    compiler told me I had a duplicate case. After investigation, I found the above
    meaning that I was able to just delete the IRGen handling.

So now we properly handle AddressOf by asserting. As an additional tactic to
make "SIL only builtins" even more explicit, I added code to the SIL verifier
that validates we never see a builtin inst that is a "SIL only builtin" and
added some comments to Builtins.def that elaborate on this.

This simplifies the function and will make it easier for me to convert the
function into an exhaustive switch with an NFC commit.
This should make it easier to add new builtins by "following the warnings" and
prevent us from not handling a builtin in IRGen.

When I did this, I discovered that if I did this naively, we would have
AddressOf show up twice in the switch. This turned out to be because:

1. AddressOf is a SIL builtin that semantically is expected to only result in
SIL being emitted instead of having a builtin "addressof" be emitted.

2. For what ever reason, we actually had code in IRGen to emit an AddressOf
BuiltinInst if we saw it (which we never should have)... but also later code
asserted that we would never see it b/c it is a "SIL only builtin".

3. When I converted the if statements to be case statements, helpfully the
compiler told me I had a duplicate case. After investigation, I found the above
meaning that I was able to just delete the IRGen handling.

So now we properly handle AddressOf by asserting. As an additional tactic to
make "SIL only builtins" even more explicit, I added code to the SIL verifier
that validates we never see a builtin inst that is a "SIL only builtin" and
added some comments to Builtins.def that elaborate on this.
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test

@gottesmm gottesmm enabled auto-merge October 24, 2025 19:13
@gottesmm
Copy link
Contributor Author

@swift-ci test windows platform

1 similar comment
@gottesmm
Copy link
Contributor Author

@swift-ci test windows platform

@gottesmm gottesmm merged commit a0d33c3 into swiftlang:main Oct 26, 2025
3 checks passed
@gottesmm gottesmm deleted the irgen-builtin-fixes branch October 26, 2025 05:09
tshortli added a commit to tshortli/swift that referenced this pull request Oct 27, 2025
Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for doing this!

case BuiltinValueKind::TypeJoinInout:
case BuiltinValueKind::TypeJoinMeta:
case BuiltinValueKind::TriggerFallbackDiagnostic:
llvm_unreachable("IRGen unimplemented for this builtin!");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reasonable builtin category we could expect these to be in? I know at least some of them are lowered in SIL passes and should never make it to IRGen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are lowered in SIL passes, we could have a category of builtins that are illegal in lowered SIL (or canonical SIL if they are lowered in Raw SIL).

Really I dislike the names of our Builtin categories at this point. I think we could make them clearer (specifically, whether or not a builtin has a builtin SIL representation should be clear from the name). Another thing that might be good to cleanup is some of the polymorphic builtin stuff. I don't know if we actually use those anywhere (and if someone wants it, we can always bring it back).

Just some thoughts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I completely agree. There are several categories that I don't remember the purpose of, and there are several categories that seem very badly named.

speednoisemovement pushed a commit to speednoisemovement/swift that referenced this pull request Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants